<template>
  <div class="purchase">
    <div class="box">
      <div class="top">
        <span>
          <i @click="close">x</i>
        </span>
      </div>

      <div class="page">
        <section>
          <div>
            <h2>日期</h2>
            <span>{{this.$store.state.date}}</span>
          </div>
          <div>
            <h2>票档</h2>
            <ul>
              <li v-for="(ticket,ins) in tickets" :key="ins">
                <span @click="choose(ticket)" :class="{active:ticket.price === price}">
                  <i>{{ticket.hint}}</i>
                  {{ticket.price}}元
                </span>
              </li>
            </ul>
          </div>
        </section>
      </div>
      <div class="ticket">
        <div>
          <span>购买数量</span>
          <el-input-number v-model="num" @change="handleChange" :min="1" :max="count" label="描述文字"></el-input-number>
        </div>
        <p>
          <span>自动分配连坐座位</span>
          <span>每人限定4张</span>
        </p>
      </div>
      <div class="bottom">
        <p class="text">请选择票档</p>
        <button class="button" @click="buy">确定</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tickets: [],
      price: 380,
      num: 1,
      count: 4,
      // 票档id
      id: "",
      getcar: "",
      // 已购买数量
      content: 0,
      seat:""
    };
  },
  mounted() {
    this.axios.get("/price?id=" + this.$store.state.id).then((res) => {
      this.tickets = res.data.result;
      this.tickets.reverse();
      this.tickets.forEach((ele) => {
        if (ele.content == 0) {
          ele.hint = "缺票登记";
        } else {
          ele.hint = "余票：" + ele.content;
        }
      });
    });
  },
  methods: {
    close() {
      this.$emit("chuan", false);
      $(document.body).css({
        "overflow-x": "scroll",
        "overflow-y": "scroll",
      });
    },
    choose(val) {
      this.id = val.id;
      this.axios
        .get(
          "/buycar?userid=" +
            this.$store.state.userId +
            "&carid=" +
            this.id +
            "&perid=" +
            this.$store.state.id
        )
        .then((res) => {
          this.getcar = res.data.result[0];
          if (val.content < 4) {
            this.count = val.content;
          } else {
            this.count = 4;
          }
          if (this.getcar) {
            this.content = this.getcar.content;
            this.count -= this.getcar.content;
            this.$message({
              message: "您已购买" + this.getcar.content + "张",
            });
          } else {
            this.content = 0;
          }
          this.num = 1;
          this.price = val.price;
          this.seat=val.seat
          $(".text").get(0).innerHTML = "￥" + val.price;
          $(".text").css({
            color: "#ff1628",
          });
          if (val.hint == "缺票登记") {
            $(".button").html("提交缺货登记");
            $(".ticket").css({
              display: "none",
            });
          } else {
            $(".button").html("购买");
            $(".ticket").css({
              display: "block",
            });
          }
          if(this.content==4){
            $('.button').attr('disabled',"true")
            $('.button').html('不可购买')  
          }else{
            $('.button').removeAttr("disabled")
          }
        });
    },
    handleChange() {
      $(".text").get(0).innerHTML = "￥" + this.price * this.num;
    },
    buy() {
      if ($(".button").html() == "购买") {
        this.close();
        this.$store.commit("seat", this.seat);
        this.$store.commit("price", this.price);
        this.$store.commit("ticketedNum", this.content);
        this.$store.commit("ticketNum", this.num);
        this.$store.commit("ticketId", this.id);
        this.$store.commit("headchange", "支付验证");
        location.href = "#/buy";
      } else {
        // 提交缺货登记
        console.log("缺货");
      }
    },
  },
};
</script>

<style scoped>
.purchase {
  position: fixed;
  z-index: 20;
  height: 100vh;
  width: 100vw;
}
.purchase .box {
  width: 100vw;
  bottom: 0;
  position: fixed;
  /*bottom: 0;*/
  background-color: white;
  border-top-right-radius: 10px;
  border-top-left-radius: 10px;
}

.purchase .box .top {
  position: fixed;
  width: 100%;
  border-top-right-radius: 10px;
  border-top-left-radius: 10px;
  background-color: white;
  margin-top: 0;
}
.purchase .box .top span {
  display: block;
  padding: 10px 0;
  /*text-align: right;*/
}
/* 关闭按钮 */
.purchase .box .top span i {
  margin-left: 300px;
  font-size: 25px;
  font-style: normal;
  color: #999;
  cursor: pointer;
}
.purchase .box .page {
  margin-top: 10px;
  text-align: left;
  padding: 0 20px;
  height: 300px;
  overflow: scroll;
}
.purchase .box .page div h2 {
  font-size: 22px;
  padding: 15px 0;
}
.purchase .box .page div:first-child {
  margin-top: 30px;
}
.purchase .box .page div:first-child span{
  background-color: rgba(255, 192, 203, 0.5);
  display: inline-block;
  padding: 15px 25px 15px 25px;
  border-radius: 5px;
  color: rgba(255, 18, 104, 0.65);
  font-size: 18px;
}
.purchase .box .page div:nth-child(2) ul {
  list-style: none;
  overflow: hidden;
}
.purchase .box .page div:nth-child(2) ul li {
  float: left;
  width: 30%;
  margin-left: 10px;
  margin-top: 10px;
}
.purchase .box .page div:nth-child(2) ul li span {
  /*width: 30%;*/
  display: block;
  background-color: rgba(153, 153, 153, 0.1);
  padding: 15px 0 15px 25px;
  border-radius: 5px;
  position: relative;
  font-size: 18px;
  cursor: pointer;
}
.purchase .box .page div:nth-child(2) ul li span.active {
  background-color: rgba(255, 117, 149, 0.27);
}
.active i {
  background-color: rgba(255, 117, 149, 0.66);
}
.purchase .box .page div:nth-child(2) ul li span i {
  position: absolute;
  font-size: 12px;
  top: 0;
  left: 0;
  font-style: normal;
  padding: 1px 5px;
  border-radius: 5px;
}
.purchase .box .ticket {
  padding: 10px 20px;
  display: none;
}
.purchase .box .ticket > div {
  overflow: hidden;
}
.purchase .box .ticket div span {
  float: left;
  font-size: 18px;
  font-weight: bolder;
  margin: 5px 0 0 15px;
}
.purchase .box .ticket div >>> .el-input-number {
  float: right;
  width: 140px;
}
.purchase .box .ticket p {
  text-align: left;
  font-size: 18px;
  color: rgba(153, 153, 153, 0.62);
}
.purchase .box .ticket p span:nth-child(2) {
  margin-left: 180px;
}

.purchase .bottom {
  overflow: hidden;
  padding: 13px 20px;
}
.purchase .bottom p {
  float: left;
  font-size: 20px;
  padding: 7px;
  color: #cbcbcb;
}
.purchase .bottom button {
  float: right;
  font-size: 20px;
  background-image: linear-gradient(90deg, #ff7f81, #ff2d79);
  border: none;
  padding: 7px 50px;
  border-radius: 20px;
  color: #fff;
}
.purchase .bottom .dis{
  background-image: linear-gradient(0,#fff,#fff) ;
}
</style>
